package com.mars.upms.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.base.BaseController;
import com.mars.base.common.base.BaseResult;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.Paginator;
import com.mars.upms.entity.UpmsPermission;
import com.mars.upms.rpc.api.UpmsPermissionService;

@RestController
@RequestMapping(value = "/api/upms/permission")
public class UpmsPermissionController extends BaseController {

	@Autowired
	private UpmsPermissionService upmsPermissionService;

	@PostMapping(value = "/add")
	public Object add(@RequestBody UpmsPermission entity) {
		try {
			if (StringUtils.isBlank(entity.getName())) {
				return BaseResult.failure("名称不能为空");
			}
			this.upmsPermissionService.insertSelective(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		try {
			this.upmsPermissionService.delete(id);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@PostMapping(value = "/update")
	public Object update(@RequestBody UpmsPermission entity) {
		try {
			if (StringUtils.isBlank(entity.getName())) {
				return BaseResult.failure("名称不能为空");
			}
			this.upmsPermissionService.update(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.upmsPermissionService.selectById(id);
	}
	
	@GetMapping(value = "/list")
	public Object list(UpmsPermission entity) {
		return this.upmsPermissionService.selectPage(new Paginator(this.request), entity);
	}

	@GetMapping(value = "/tree")
	public Object tree(UpmsPermission entity) {
		return this.upmsPermissionService.tree(entity);
	}
}
